#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/stat.h>
#include "syntax.h"
#include "io.h"
#include "mem_macros.h"
#include "mem_structures.h"
#include "mem_functions.h"

#define PRINT(a) printf("%s\n",a);
#define MSG_SIZE 250
void print_buffer(buffer_t* buffer);

static POSITION p;

int main(int argc, char **argv)
{
    POSITION p = 1;
    int i=0;
    int j=0;
    int ret;
    buffer_t* buffer;
    chunk_t* chunk;
    sFile* sf;
    char twenty[20] = { 'a','b','c','d','e',
                        'f','g','h','i','j',
                        'k','l','m','n','o',
                        'p','q','r','s','\n'};


    if(argc>2)
    {
        printf("RTFC\n");
        return 1;
    }

    if(argc==2)
    {
        sf = statFile(argv[1]);
        if(sf==NULL)
        {
            printf("Error using file '%s'\n",argv[1]);
            return 1;
        }

        ret = readFile(sf);
        if(ret!=0)
        {
            DEBUG_FATAL("Could not read file");
        }
        buffer = sf->buffer;
        print_buffer(buffer);
        mem_delBuffer(buffer);
    }

    if(argc==1)
    {

        buffer = mem_newBuffer();

        /* CHECK SIMPLE CHARACTER ADDING */
        printf("Adding 'a' to buffer at position [%i]\n",p);
        mem_addChar(buffer,'a',p++);
        print_buffer(buffer);

        /* CHECK Adding more characters */
        printf("Adding 20 characters to buffer\n");
        for(i=0;i<20;i++)
        {
            mem_addChar(buffer,twenty[i],p++);
        }
        print_buffer(buffer);

        /* CHECK Adding more characters into existing character stream*/
        printf("Adding 20 characters to middle of buffer\n");
        p = 10;
        for(i=0;i<20;i++)
        {
            mem_addChar(buffer,twenty[i],p++);
        }

        printf("Adding characters\n");
        for(i=0;i<25;i++)
        {
            for(j=0;j<20;j++)
            {
                mem_addChar(buffer,twenty[j],p++);
            }
        }

        print_buffer(buffer);
        mem_delBuffer(buffer);
    }
}

void print_buffer(buffer_t* buffer)
{
    /* Assertions */
    assert(buffer!=NULL);

    /* Safe Buffer */
    int i;
    char *print_buffer = calloc(CHUNK_SIZE*10,sizeof(char));

    chunk_t* chunk = buffer->head;
    assert(chunk!=NULL);
    assert(chunk->mem!=NULL);

    printf("Buffer Current Has [%i] Chunks\n",buffer->num_chunks);

    printf("Printing Buffer:\n");

    while(chunk!=NULL)
    {
        snprintf(print_buffer,CHUNK_SIZE,"%s",chunk->mem);
        printf("%s",print_buffer);
        chunk=chunk->next;
    }

    printf("\n");

    free(print_buffer);
}

void print_buffer_chunks(buffer_t* buffer)
{
    /* Assertions */
    assert(buffer!=NULL);

    /* Safe Buffer */
    int i;
    char *print_buffer = calloc(CHUNK_SIZE*10,sizeof(char));

    chunk_t* chunk = buffer->head;
    assert(chunk!=NULL);
    assert(chunk->mem!=NULL);

    printf("Buffer Current Has [%i] Chunks\n",buffer->num_chunks);

    printf("Printing Buffer:\n");

    for(i=0;chunk!=NULL;i++)
    {
        snprintf(print_buffer,CHUNK_SIZE+MSG_SIZE,"\nChunk [%i] : %s",i,chunk->mem);
        printf("%s",print_buffer);
        chunk=chunk->next;
    }

    printf("\n");


}
